package pl.plesken.xml;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.util.LinkedList;

import noNamespace.DetailsDocument;
import noNamespace.DetailsDocument.Details.Movie;
import noNamespace.DetailsDocument.Details.Movie.Cast;
import noNamespace.DetailsDocument.Details.Movie.Files;
import noNamespace.DetailsDocument.Details.Movie.Sets;
import noNamespace.DetailsDocument.Details.Movie.Trailers;
import noNamespace.DetailsDocument.Details.Movie.Files.File.FilePlot;

import org.apache.xmlbeans.XmlException;
import org.apache.xmlbeans.XmlObject;
import org.apache.xmlbeans.XmlOptions;


public class EditXMLFile {

	String Preferences;
	String IsTrailer;
	String FileName;
	String Title;
	String TitleSort;
	String OriginalTitle;
	String Year;
	String ReleaseDate;
	String Rating;
	String Top;
	String Details;
	String PosterURL;
	String DetailPoster;
	String PosterSubImg;
	String PosterFile;
	String Thumbnail;
	String FanartFile;
	String FanartURL;
	String Plot;
	String Outline;
	String Director;
	String Country;
	String Company;
	String Runtime;
	String Certification;
	String Season;
	String Language;
	String Subtitles;
	String TrailerExchange;
	String Container;
	String VideoCodec;
	String AudioCodec;
	String AudioChannels;
	String Resolution;
	String VideoSource;
	String VideoOutput;
	String Fps;
	String First;
	String Previous;
	String Next;
	String Last;
	String LibraryDesc;
	String Prebuf;
	StringBuffer GenresT = new StringBuffer();
	String Genres;
	StringBuffer ActorsT = new StringBuffer();
	String Actors;
	StringBuffer FilesT = new StringBuffer();
	LinkedList<String> rowFiles = new LinkedList<String>();
	String Box;
	

	public String getPosterFile() {
		return PosterFile;
	}

	public void setPosterFile(String posterFile) {
		PosterFile = posterFile;
	}

	public String getFanartURL() {
		return FanartURL;
	}

	public void setFanartURL(String fanartURL) {
		FanartURL = fanartURL;
	}

	public LinkedList<String> getRowFiles() {
		return rowFiles;
	}

	public void setRowFiles(LinkedList<String> rowFiles) {
		this.rowFiles = rowFiles;
	}

	public String getActors() {
		return Actors;
	}

	public void setActors(String actors) {
		Actors = actors;
	}

	public String getGenres() {
		return Genres;
	}

	public void setGenres(String genres) {
		Genres = genres;
	}

	public String getPreferences() {
		return Preferences;
	}

	public void setPreferences(String preferences) {
		Preferences = preferences;
	}

	public String getFileName() {
		return FileName;
	}

	public void setFileName(String fileName) {
		FileName = fileName;
	}

	public String getTitle() {
		return Title;
	}

	public void setTitle(String title) {
		Title = title;
	}

	public String getTitleSort() {
		return TitleSort;
	}

	public void setTitleSort(String titleSort) {
		TitleSort = titleSort;
	}

	public String getOriginalTitle() {
		return OriginalTitle;
	}

	public void setOriginalTitle(String originalTitle) {
		OriginalTitle = originalTitle;
	}

	public String getYear() {
		return Year;
	}

	public void setYear(String year) {
		Year = year;
	}

	public String getReleaseDate() {
		return ReleaseDate;
	}

	public void setReleaseDate(String releaseDate) {
		ReleaseDate = releaseDate;
	}

	public String getRating() {
		return Rating;
	}

	public void setRating(String rating) {
		Rating = rating;
	}

	public String getTop() {
		return Top;
	}

	public void setTop(String top) {
		Top = top;
	}

	public String getDetails() {
		return Details;
	}

	public void setDetails(String details) {
		Details = details;
	}

	public String getPosterURL() {
		return PosterURL;
	}

	public void setPosterURL(String posterURL) {
		PosterURL = posterURL;
	}

	public String getDetailPoster() {
		return DetailPoster;
	}

	public void setDetailPoster(String detailPoster) {
		DetailPoster = detailPoster;
	}

	public String getPosterSubImg() {
		return PosterSubImg;
	}

	public void setPosterSubImg(String posterSubImg) {
		PosterSubImg = posterSubImg;
	}

	public String getThumbnail() {
		return Thumbnail;
	}

	public void setThumbnail(String thumbnail) {
		Thumbnail = thumbnail;
	}

	public String getFanartFile() {
		return FanartFile;
	}

	public void setFanartFile(String fanartFile) {
		FanartFile = fanartFile;
	}

	public String getPlot() {
		return Plot;
	}

	public void setPlot(String plot) {
		Plot = plot;
	}

	public String getOutline() {
		return Outline;
	}

	public void setOutline(String outline) {
		Outline = outline;
	}

	public String getDirector() {
		return Director;
	}

	public void setDirector(String director) {
		Director = director;
	}

	public String getCountry() {
		return Country;
	}

	public void setCountry(String country) {
		Country = country;
	}

	public String getCompany() {
		return Company;
	}

	public void setCompany(String company) {
		Company = company;
	}

	public String getRuntime() {
		return Runtime;
	}

	public void setRuntime(String runtime) {
		Runtime = runtime;
	}

	public String getCertification() {
		return Certification;
	}

	public void setCertification(String certification) {
		Certification = certification;
	}

	public String getSeason() {
		return Season;
	}

	public void setSeason(String season) {
		Season = season;
	}

	public String getLanguage() {
		return Language;
	}

	public void setLanguage(String language) {
		Language = language;
	}

	public String getSubtitles() {
		return Subtitles;
	}

	public void setSubtitles(String subtitles) {
		Subtitles = subtitles;
	}

	public String getTrailerExchange() {
		return TrailerExchange;
	}

	public void setTrailerExchange(String trailerExchange) {
		TrailerExchange = trailerExchange;
	}

	public String getContainer() {
		return Container;
	}

	public void setContainer(String container) {
		Container = container;
	}

	public String getVideoCodec() {
		return VideoCodec;
	}

	public void setVideoCodec(String videoCodec) {
		VideoCodec = videoCodec;
	}

	public String getAudioCodec() {
		return AudioCodec;
	}

	public void setAudioCodec(String audioCodec) {
		AudioCodec = audioCodec;
	}

	public String getAudioChannels() {
		return AudioChannels;
	}

	public void setAudioChannels(String audioChannels) {
		AudioChannels = audioChannels;
	}

	public String getResolution() {
		return Resolution;
	}

	public void setResolution(String resolution) {
		Resolution = resolution;
	}

	public String getVideoSource() {
		return VideoSource;
	}

	public void setVideoSource(String videoSource) {
		VideoSource = videoSource;
	}

	public String getVideoOutput() {
		return VideoOutput;
	}

	public void setVideoOutput(String videoOutput) {
		VideoOutput = videoOutput;
	}

	public String getFps() {
		return Fps;
	}

	public void setFps(String fps) {
		Fps = fps;
	}

	public String getFirst() {
		return First;
	}

	public void setFirst(String first) {
		First = first;
	}

	public String getPrevious() {
		return Previous;
	}

	public void setPrevious(String previous) {
		Previous = previous;
	}

	public String getNext() {
		return Next;
	}

	public void setNext(String next) {
		Next = next;
	}

	public String getLast() {
		return Last;
	}

	public void setLast(String last) {
		Last = last;
	}

	public String getLibraryDesc() {
		return LibraryDesc;
	}

	public void setLibraryDesc(String libraryDesc) {
		LibraryDesc = libraryDesc;
	}

	public String getPrebuf() {
		return Prebuf;
	}

	public void setPrebuf(String prebuf) {
		Prebuf = prebuf;
	}
	
	public String getBox() {
		return Box;
	}

	public void setBox(String box) {
		Box = box;
	}

	
	public DetailsDocument loadData(String file) throws XmlException, IOException {
		File xmlFile = new File(file);
		DetailsDocument details = DetailsDocument.Factory.parse(xmlFile);
		return details;
	}
	
	public XmlObject preferences(DetailsDocument details) {
		XmlObject val = details.getDetails().getPreferences();
		return val;
	}
	
	public String skinDir(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getMjbSkinDir();
		return val;
	}
	
	public String postersPerspective(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getPostersPerspective();
		return val;
	}
	
	public String posterWidth(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getPostersWidth();
		return val;
	}
	
	public String posterHeight(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getPostersHeight();
		return val;
	}
	
	public String thumbnailWidth(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getThumbnailsWidth();
		return val;
	}
	
	public String thumbnailHeight(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getThumbnailsHeight();
		return val;
	}
	
	public String perspectiveTopPoster(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getPostersPerspectiveTop();
		return val;
	}

	public String perspectiveBottomPoster(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getPostersPerspectiveBottom();
		return val;
	}

	public String perspectiveTopThumbnail(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getThumbnailsPerspectiveTop();
		return val;
	}

	public String perspectiveBottomThumbnail(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getThumbnailsPerspectiveBottom();
		return val;
	}
	
	public String posterReflectionHeight(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getPostersReflectionHeight();
		return val;
	}
	
	public String thumbnailReflectionHeight(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getThumbnailsReflectionHeight();
		return val;
	}

	public Boolean perspectiveThumbnail(DetailsDocument details) {
		Boolean val = Boolean.valueOf(details.getDetails().getPreferences().getThumbnailsPerspective());
		return val;
	}
	
	public String perspectiveDirection(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getPostersPerspectiveDirection();
		return val;
	}

	public Boolean logoHD(DetailsDocument details) {
		Boolean val = Boolean.valueOf(details.getDetails().getPreferences().getPostersLogoHD());
		return val;
	}

	public Boolean logoTV(DetailsDocument details) {
		Boolean val = Boolean.valueOf(details.getDetails().getPreferences().getPostersLogoTV());
		return val;
	}
	
	public Boolean logoThumbHD(DetailsDocument details) {
		Boolean val = Boolean.valueOf(details.getDetails().getPreferences().getThumbnailsLogoHD());
		return val;
	}

	public Boolean logoThumbTV(DetailsDocument details) {
		Boolean val = Boolean.valueOf(details.getDetails().getPreferences().getThumbnailsLogoTV());
		return val;
	}
	
	public Boolean logoLang(DetailsDocument details) {
		Boolean val = Boolean.valueOf(details.getDetails().getPreferences().getPostersLanguage());
		return val;
	}
	
	public Boolean posterReflection(DetailsDocument details) {
		Boolean val = Boolean.valueOf(details.getDetails().getPreferences().getPostersReflection());
		return val;
	}
	
	public Boolean thumbnailReflection(DetailsDocument details) {
		Boolean val = Boolean.valueOf(details.getDetails().getPreferences().getThumbnailsReflection());
		return val;
	}

	public String thumbnailFormat(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getThumbnailsFormat();
		return val;
	}

	public String postersFormat(DetailsDocument details) {
		String val = details.getDetails().getPreferences().getPostersFormat();
		return val;
	}
	
	public String isTrailer(DetailsDocument details) {
		String val = details.getDetails().getMovie().getIsTrailer();
		return val;
	}
	
	public String fileName(DetailsDocument details) {
		String val = details.getDetails().getMovie().getBaseFilename();
		return val;
	}
	
	public String title(DetailsDocument details) {
		String val = details.getDetails().getMovie().getTitle();
		return val;
	}
	
	public String titleSort(DetailsDocument details) {
		String val = details.getDetails().getMovie().getTitleSort();
		return val;
	}
	
	public String originalTitle(DetailsDocument details) {
		String val = details.getDetails().getMovie().getOriginalTitle();
		return val;
	}
	
	public String year(DetailsDocument details) {
		String val = String.valueOf(details.getDetails().getMovie().getYear());
		return val;
	}
	
	public String releaseDate(DetailsDocument details) {
		String val = details.getDetails().getMovie().getReleaseDate();
		return val;
	}
	
	public String rating(DetailsDocument details) {
		String val = String.valueOf(details.getDetails().getMovie().getRating());
		return val;
	}
	
	public String top(DetailsDocument details) {
		String val = String.valueOf(details.getDetails().getMovie().getTop250());
		return val;
	}
	
	public String details(DetailsDocument details) {
		String val = String.valueOf(details.getDetails().getMovie().getDetails());
		return val;
	}
	
	public String posterURL(DetailsDocument details) {
		String val = details.getDetails().getMovie().getPosterURL().replace("%3A", ":").replace("%2F", "/");
		return val;
	}
	
	public String posterSubImg(DetailsDocument details) {
		String val = details.getDetails().getMovie().getPosterSubimage();
		return val;
	}

	public String posterFile(DetailsDocument details) {
		String val = details.getDetails().getMovie().getPosterFile();
		return val;
	}
	
	public String detailPosterFile(DetailsDocument details) {
		String val = details.getDetails().getMovie().getDetailPosterFile();
		return val;
	}
	
	public String thumbnail(DetailsDocument details) {
		String val = details.getDetails().getMovie().getThumbnail();
		return val;
	}
	
	public String fanartFile(DetailsDocument details) {
		String val = details.getDetails().getMovie().getFanartFile();
		return val;
	}
	
	public String plot(DetailsDocument details) {
		String val = details.getDetails().getMovie().getPlot();
		return val;
	}
	
	public String outline(DetailsDocument details) {
		String val = details.getDetails().getMovie().getOutline();
		return val;
	}
	
	public String director(DetailsDocument details) {
		String val = details.getDetails().getMovie().getDirector();
		return val;
	}
	
	public String country(DetailsDocument details) {
		String val = details.getDetails().getMovie().getCountry();
		return val;
	}
	
	public String company(DetailsDocument details) {
		String val = details.getDetails().getMovie().getCompany();
		return val;
	}
	
	public String runtime(DetailsDocument details) {
		String val = String.valueOf(details.getDetails().getMovie().getRuntime());
		return val;
	}
	
	public String certification(DetailsDocument details) {
		String val = details.getDetails().getMovie().getCertification();
		return val;
	}
	
	public String season(DetailsDocument details) {
		String val = String.valueOf(details.getDetails().getMovie().getSeason());
		return val;
	}
	
	public String language(DetailsDocument details) {
		String val = details.getDetails().getMovie().getLanguage();
		return val;
	}
	
	public String subtitles(DetailsDocument details) {
		String val = details.getDetails().getMovie().getSubtitles();
		return val;
	}
	
	public String trailerExchange(DetailsDocument details) {
		String val = details.getDetails().getMovie().getTrailerExchange();
		return val;
	}
	
	public String container(DetailsDocument details) {
		String val = details.getDetails().getMovie().getContainer();
		return val;
	}
	
	public String videoCodec(DetailsDocument details) {
		String val = details.getDetails().getMovie().getVideoCodec();
		return val;
	}
	
	public String audioCodec(DetailsDocument details) {
		String val = details.getDetails().getMovie().getAudioCodec();
		return val;
	}
	
	public String audioChannels(DetailsDocument details) {
		String val = String.valueOf(details.getDetails().getMovie().getAudioChannels());
		return val;
	}
	
	public String resolution(DetailsDocument details) {
		String val = details.getDetails().getMovie().getResolution();
		return val;
	}
	
	public String videoSource(DetailsDocument details) {
		String val = details.getDetails().getMovie().getVideoSource();
		return val;
	}
	
	public String videoOutput(DetailsDocument details) {
		String val = details.getDetails().getMovie().getVideoOutput();
		return val;
	}
	
	public String fps(DetailsDocument details) {
		String val = String.valueOf(details.getDetails().getMovie().getFps());
		return val;
	}
	
	public String first(DetailsDocument details) {
		String val = details.getDetails().getMovie().getFirst();
		return val;
	}
	
	public String previous(DetailsDocument details) {
		String val = details.getDetails().getMovie().getPrevious();
		return val;
	}
	
	public String next(DetailsDocument details) {
		String val = details.getDetails().getMovie().getNext();
		return val;
	}
	
	public String last(DetailsDocument details) {
		String val = details.getDetails().getMovie().getLast();
		return val;
	}
	
	public String libraryDesc(DetailsDocument details) {
		String val = details.getDetails().getMovie().getLibraryDescription();
		return val;
	}
	
	public String prebuf(DetailsDocument details) {
		String val = String.valueOf(details.getDetails().getMovie().getPrebuf());
		return val;
	}
	
	public String fanartURL(DetailsDocument details) {
		String val = details.getDetails().getMovie().getFanartURL();
		return val;
	}
	
	public String setName(DetailsDocument details) {
		String val = details.getDetails().getMovie().getSets().getSetArray(0).getStringValue();
		return val;
	}
	
	public String genres(DetailsDocument details) {
		GenresT.setLength(0);
		
		String[] genres = details.getDetails().getMovie().getGenres().getGenreArray();
				
		for (int i = 0; i < genres.length; i++) {
			String genre = details.getDetails().getMovie().getGenres().getGenreArray(i);
			if (i == 0) {
				GenresT.append(genre);
			} else {
				GenresT.append(";" + genre);
			}
		}
		return GenresT.toString();
	}
	
	public String box(DetailsDocument details) {
		StringBuffer box = new StringBuffer();
		box.setLength(0);
		
		if (details.getDetails().getMovie().getSets() != null) {
			DetailsDocument.Details.Movie.Sets.Set[] sets = details.getDetails().getMovie().getSets().getSetArray();
		
			for (int a = 0; a < sets.length; a++) {
				String setName = details.getDetails().getMovie().getSets().getSetArray(a).getStringValue();
				String setOrder = details.getDetails().getMovie().getSets().getSetArray(a).getOrder();
				if (a == 0) {
					box.append(setName + "<ORDER>" + setOrder);
				} else {
					box.append("<BREAK>" + setName + "<ORDER>" + setOrder);
				}
			}
		}
		return box.toString();
	}
	
	public String actors(DetailsDocument details) {
		ActorsT.setLength(0);
		
		String[] actors = details.getDetails().getMovie().getCast().getActorArray();
		
		for (int i = 0; i < actors.length; i++) {
			String val = details.getDetails().getMovie().getCast().getActorArray(i);
			if (i == 0) {
				ActorsT.append(val);
			} else {
				ActorsT.append(";" + val);
			}
		} 
		return ActorsT.toString();
	}
	
	public void files(String file) {
		try {
			File xmlFile = new File(file);
			
			DetailsDocument details = DetailsDocument.Factory.parse(xmlFile);
			
			FilesT.setLength(0);
			Files.File[] files = details.getDetails().getMovie().getFiles().getFileArray();
		
			rowFiles.clear();
			
			StringBuffer plot = new StringBuffer();
			
			for (int i = 0; i < files.length; i++) {
				String part = details.getDetails().getMovie().getFiles().getFileArray(i).getFirstPart().toString();
				String title = details.getDetails().getMovie().getFiles().getFileArray(i).getTitle().toString();
				String url = details.getDetails().getMovie().getFiles().getFileArray(i).getFileURL().toString();
				String excSubt = details.getDetails().getMovie().getFiles().getFileArray(i).getSubtitlesExchange().toString();
				if (details.getDetails().getMovie().getFiles().getFileArray(i).getFilePlot() == null) {
					plot.append("Plot is unavailable");
				} else {
					plot.append(details.getDetails().getMovie().getFiles().getFileArray(i).getFilePlot().getStringValue());
				}
				String val = part + "<BREAK_TABLE>" + title + "<BREAK_TABLE>" + url + "<BREAK_TABLE>" + excSubt + "<BREAK_TABLE>" + plot.toString();
				rowFiles.add(val);
				plot.setLength(0);
			}
		} catch(XmlException e) {
			System.out.println(e);
		} catch(IOException e) {
			System.out.println(e);
		} catch(NullPointerException e) {
			e.printStackTrace();
		}
	}
	
	public void getParameters(String file) {
		try {
			EditXMLFile xml = new EditXMLFile();
			DetailsDocument details = xml.loadData(file);
			
			FileName = xml.fileName(details);
			Title = xml.title(details);
			TitleSort = xml.titleSort(details);
			OriginalTitle = xml.originalTitle(details);
			Year = xml.year(details);
			ReleaseDate = xml.releaseDate(details);
			Rating = xml.rating(details);
			Top = xml.rating(details);
			Details = xml.details(details);
			DetailPoster = xml.detailPosterFile(details);
			PosterURL = xml.posterURL(details);
			PosterSubImg = xml.posterSubImg(details);
			Thumbnail = xml.thumbnail(details);
			FanartFile = xml.fanartFile(details);
			Plot = xml.plot(details);
			Outline = xml.outline(details);
			Director = xml.director(details);
			Country = xml.country(details);
			Company = xml.company(details);
			Runtime = xml.runtime(details);
			Certification = xml.certification(details);
			Season = xml.season(details);
			Language = xml.language(details);
			Subtitles = xml.subtitles(details);
			TrailerExchange = xml.trailerExchange(details);
			Container = xml.container(details);
			VideoCodec = xml.videoCodec(details);
			AudioCodec =  xml.audioCodec(details);
			AudioChannels =  xml.audioChannels(details);
			Resolution = xml.resolution(details);
			VideoSource = xml.videoSource(details);
			VideoOutput = xml.videoOutput(details);
			Fps = xml.fps(details);
			First = xml.first(details);
			Previous = xml.previous(details);
			Next = xml.next(details);
			Last = xml.last(details);
			LibraryDesc = xml.libraryDesc(details);
			Prebuf = xml.prebuf(details);		
			Genres = xml.genres(details);
			Actors = xml.actors(details);
			FanartURL = xml.fanartURL(details);
			PosterFile = xml.posterFile(details);
			Thumbnail = xml.thumbnail(details);
			Box = xml.box(details);
			
		} catch(XmlException e) {
			System.out.println(e);
		} catch(IOException e) {
			System.out.println(e);
		} catch(NullPointerException e) {
		}
	}
	
	public void saveXMLFile(String file) {
		try {
			EditXMLFile xml = new EditXMLFile();
			DetailsDocument load = xml.loadData(file);
			DetailsDocument details = DetailsDocument.Factory.newInstance();
			DetailsDocument.Details values = DetailsDocument.Details.Factory.newInstance();
			DetailsDocument.Details.Preferences pref = DetailsDocument.Details.Preferences.Factory.newInstance();
			
			pref.set(xml.preferences(load)); 
			
			Movie params = Movie.Factory.newInstance();   
			
			params.setIsTrailer(xml.isTrailer(load));
			params.setIdArray(load.getDetails().getMovie().getIdArray());
			params.setBaseFilename(load.getDetails().getMovie().getBaseFilename());
			params.setTitle(Title);
			params.setTitleSort(Title);
			params.setOriginalTitle(OriginalTitle);
			params.setYear(Year);
			params.setReleaseDate(ReleaseDate);
			params.setRating(Rating);
			params.setTop250(load.getDetails().getMovie().getTop250());
			params.setDetails(load.getDetails().getMovie().getDetails());
			params.setPosterURL(PosterURL);
			params.setPosterSubimage(load.getDetails().getMovie().getPosterSubimage());
			params.setFanartURL(FanartURL);
			params.setPosterFile(PosterFile);
			params.setDetailPosterFile(DetailPoster);
			params.setThumbnail(Thumbnail);
			params.setFanartFile(FanartFile);
			params.setPlot(Plot);
			params.setOutline(Outline);
			params.setDirector(Director);
			params.setCountry(Country);
			params.setRuntime(Runtime);
			params.setCertification(load.getDetails().getMovie().getCertification());
			params.setSeason(load.getDetails().getMovie().getSeason());
			params.setLanguage(Language);
			params.setSubtitles(Subtitles);
			params.setTrailerExchange(load.getDetails().getMovie().getTrailerExchange());
			params.setContainer(Container);
			params.setVideoCodec(VideoCodec);
			params.setAudioCodec(AudioCodec);
			params.setAudioChannels(load.getDetails().getMovie().getAudioChannels());
			params.setResolution(Resolution);
			params.setVideoSource(VideoSource);
			params.setVideoOutput(VideoOutput);
			params.setFps(Fps);
			params.setFirst(load.getDetails().getMovie().getFirst());
			params.setPrevious(load.getDetails().getMovie().getPrevious());
			params.setNext(load.getDetails().getMovie().getNext());
			params.setLast(load.getDetails().getMovie().getLast());
			params.setLibraryDescription(load.getDetails().getMovie().getLibraryDescription());
			params.setPrebuf(load.getDetails().getMovie().getPrebuf());
			
			//if (load.getDetails().getMovie().getSets() != null) {
			if (Box.length() > 0) {
				Sets sets = Sets.Factory.newInstance();
				//sets.setSetArray(load.getDetails().getMovie().getSets().getSetArray());
				String[] set = Box.split("\n");
				for (int i = 0; i < set.length; i++) {
					Sets.Set s = Sets.Set.Factory.newInstance();
					if (set[i].contains("-")) {
						String[] sep = set[i].split("-");
						s.setStringValue(sep[0]);
						s.setOrder(sep[1]);
					} else {
						s.setStringValue(set[i]);
					}
					sets.addNewSet();
					sets.setSetArray(i, s);
				}
				params.setSets(sets);
			}
			
			Movie.Genres genres = Movie.Genres.Factory.newInstance();
			genres.setGenreArray(Genres.split("\n"));
			params.setGenres(genres);
			
			Cast cast = Cast.Factory.newInstance();
			cast.setActorArray(Actors.split("\n"));
			params.setCast(cast);
			
			Files files = Files.Factory.newInstance();
			
			for (int i = 0; i < rowFiles.size(); i++) {
				Files.File f = Files.File.Factory.newInstance();
				FilePlot plot = FilePlot.Factory.newInstance();
				String[] tab = rowFiles.get(i).split("<BREAK>");
				f.setFirstPart(tab[0]);
				f.setLastPart(tab[0]);
				f.setTitle(tab[1]);
				f.setSubtitlesExchange(tab[3]);
				f.addNewFilePlot();
				plot.setPart(tab[0]);
				plot.setStringValue(tab[4]);
				f.setFilePlot(plot);
				f.setFileURL(tab[2]);
				
				if (load.getDetails().getMovie().getFiles().getFileArray(i).getFileImageFile() != null) {
					f.setFileImageFile(load.getDetails().getMovie().getFiles().getFileArray(i).getFileImageFile());
				}
				if (load.getDetails().getMovie().getFiles().getFileArray(i).getFileImageURL() != null) {
					f.setFileImageURL(load.getDetails().getMovie().getFiles().getFileArray(i).getFileImageURL());
				}
				
				files.addNewFile();
				files.setFileArray(i, f);
			}
		
			if (load.getDetails().getMovie().getTrailers() != null) {
				Trailers trailers = Trailers.Factory.newInstance();
				trailers.setTrailerArray(load.getDetails().getMovie().getTrailers().getTrailerArray());
				params.setTrailers(trailers);
			}
			
			params.setFiles(files);
			values.setPreferences(pref);
			values.setMovie(params);
			details.setDetails(values);
			
			File out = new File(file);
			OutputStream os = new FileOutputStream(out);
			
			XmlOptions options = new XmlOptions();
			options.setSavePrettyPrint();
			details.save(out, options);
			os.flush();
			
		}catch(XmlException e) {
			System.out.println(e);
		} catch(IOException e) {
			System.out.println(e);
		} catch(NullPointerException e) {
		}
	}
	
}
